package com.example.controller;

import com.example.DTO.DataSetDTO;
import com.example.DTO.DataSetPageQueryDTO;
import com.example.VO.DataSetVO;
import com.example.service.DataSetService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import com.example.result.*;

import java.util.List;

@RestController
@RequestMapping("/dataset")
@Api(tags = "数据集管理接口")
@Slf4j
public class DataSetController {

    @Autowired
    private DataSetService datasetService;

    @PostMapping
    @ApiOperation("新增数据集")
    public Result save(@RequestBody DataSetDTO dataSetDTO) {
        log.info("新增数据集:{}", dataSetDTO);
        datasetService.save(dataSetDTO);
        return Result.success();
    }

    /**
     * 数据集分页查询
     *
     * @param dataSetPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("数据集分页查询")
    public Result<PageResult> page(DataSetPageQueryDTO dataSetPageQueryDTO) {
        log.info("数据集分页查询:{}", dataSetPageQueryDTO);
        PageResult pageResult = datasetService.pageQuery(dataSetPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除数据集
     *
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除数据集")
    public Result<String> deleteById(Long id){
        log.info("删除数据集：{}", id);
        datasetService.deleteById(id);
        return Result.success();
    }

    /**
     * 根据id查询数据集，用于修改页面回显数据
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询数据集")
    public Result<DataSetVO> getById(@PathVariable Long id) {
        DataSetVO dataSetVO = datasetService.getById(id);
        return Result.success(dataSetVO);
    }

    /**
     * 修改数据集
     *
     * @param dataSetDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改数据集")
    public Result update(@RequestBody DataSetDTO dataSetDTO) {
        datasetService.update(dataSetDTO);
        return Result.success();
    }
}
